package 
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.net.GroupSpecifier;
	import flash.net.NetConnection;
	import flash.net.NetGroup;
	import flash.net.NetStream;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Marc Andre Girard
	 */
	public class Main extends Sprite 
	{
		
		//private const CirrusAddress:String = "rtmfp://p2p.rtmfp.net";
		private const CIRRUS_ADDRESS:String = "rtmfp://p2p.rtmfp.net/d67901d8e650b976dd013c43-727b43edda61/";
		//private const DeveloperKey:String = "your-developer-key";
		private const DEVELOPPER_KEY:String = "d67901d8e650b976dd013c43-727b43edda61";
		
		
		private var mNetConnection:NetConnection;
		private var mSendText:Sprite;
		private var mTextfield:TextField;
		 
		private var _groupSpecifier:GroupSpecifier = new GroupSpecifier("MY_ROOM_ID_0");
		
		private var _outgoingStream:NetStream; 
		
		private var _netGroup:NetGroup; 
		private var mFullTextfield:TextField;



		public function Main():void 
		{
			if (stage) 
			{
				Init();
			}
			else 
			{
				addEventListener(Event.ADDED_TO_STAGE, Init);
			}
		}
		
		private function Init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, Init);
			
			mNetConnection = new NetConnection();
			mNetConnection.addEventListener(NetStatusEvent.NET_STATUS, OnNetConnectionStatus);
			mNetConnection.connect(CIRRUS_ADDRESS, DEVELOPPER_KEY);
		}
		
		private function OnNetConnectionStatus(aEvent:NetStatusEvent):void 
		{
			aEvent.info.code
			mNetConnection.removeEventListener(NetStatusEvent.NET_STATUS, OnNetConnectionStatus);
			mNetConnection.nearID
			
			DoSomething();
			
			InitUI();
		}
		
		private function DoSomething():void 
		{
			_groupSpecifier.multicastEnabled = true;
			_groupSpecifier.objectReplicationEnabled = true;
			_groupSpecifier.postingEnabled = true; 
			_groupSpecifier.routingEnabled = true; 
			 
			// When set to "true", Cirrus can help clients find each other. 
			_groupSpecifier.serverChannelEnabled = true; 
			 
			// Additional GroupSpecifier Options 

			// When set to "true", the Flash Player instance will send 
			// membership updates on a LAN to inform other LAN-connected group 
			// neighbors of their participation.
			_groupSpecifier.ipMulticastMemberUpdatesEnabled = true; 
			_groupSpecifier.setPostingPassword("mypassword", "salt");
			// When set to "true", it will disable the Peer-assisted Networking 
			// dialog and P2P connections with other group members. Useful 
			// for pure IP-multicast scenarios.
			//_groupSpecifier.peerToPeerDisabled = true;
			
			//
			// We create a stream in our net connection for P2P outgoing 
			//_outgoingStream = new NetStream( mNetConnection, NetStream.DIRECT_CONNECTIONS );
			// We add a net status listener to the NetStream
			//_outgoingStream.addEventListener( NetStatusEvent.NET_STATUS, onNetStatus );
			
			// Creating a NetGroup instance
			_netGroup = new NetGroup( mNetConnection, _groupSpecifier.groupspecWithAuthorizations() );
			// Adding a net status listener
			_netGroup.addEventListener( NetStatusEvent.NET_STATUS, onNetGroupStatus );
		}
		
		private function onNetGroupStatus(e:NetStatusEvent):void 
		{
			if (!(e && e.info && e.info.code))
			{
				return;
			}
			switch(e.info.code)
			{
				case "NetGroup.Neighbor.Disconnect":
				{
					AddText("Someone left the room");
					break;
				}
				case "NetGroup.Neighbor.Connect":
				{
					AddText("Someone joined the room");
					break;
				}
				case "NetGroup.Posting.Notify":
				{
					AddText(e.info.message as String);
					break;
				}
			}
		}
		
		private function AddText(object:String):void 
		{
			mFullTextfield.appendText(object + "\n");
		}
		
		private function onNetStatus(e:NetStatusEvent):void 
		{
			var test:String = "";
		}
		
		private function InitUI():void 
		{
			mSendText = new Sprite();
			mSendText.graphics.beginFill(0x000000, 1);
			mSendText.graphics.drawRect(0, 0, 100, 40);
			mSendText.graphics.endFill();
			
			
			mFullTextfield = new TextField();
			mFullTextfield.type = TextFieldType.DYNAMIC;
			mFullTextfield.border = true;
			mFullTextfield.text = "Full Text here";
			mFullTextfield.width = 500;
			mFullTextfield.height = 400;
			
			mTextfield = new TextField();
			mTextfield.type = TextFieldType.INPUT;
			mTextfield.border = true;
			mTextfield.text = "test";
			mTextfield.width = 400;
			var space:int = 20;
			
			addChild(mSendText);
			addChild(mTextfield);
			addChild(mFullTextfield);
			
			mTextfield.x = space;
			mTextfield.y = 50;
			mTextfield.addEventListener(KeyboardEvent.KEY_DOWN, OnTextfieldKeyDown);
			
			mTextfield.x = space;
			mTextfield.y = 500;
			
			mSendText.x = mTextfield.width + mTextfield.x + space;
			mSendText.y = 500;
			
			mSendText.addEventListener(MouseEvent.CLICK, OnSendButtonClicked);
		}
		
		private function OnTextfieldKeyDown(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				SendText();
			}
		}
		
		private function OnSendButtonClicked(e:MouseEvent):void 
		{
			SendText();
		}
		
		private function SendText():void 
		{
			AddText(mTextfield.text);
			var t:String = _netGroup.post(mTextfield.text);
			mTextfield.text = "";
		}
		
	}
	
}